package com.ruoyi.web.service.impl;

import com.baidubce.BceClientConfiguration;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.cdn.CdnClient;
import com.baidubce.services.cdn.model.*;

public class Sample {
    public static void main(String[] args) {
        String endPoint = "http://cdn.baidubce.com";                     // CDN服务端接口地址
        String accessKeyId = "ALTAKC39x5Nphn8T5whwvOs8hE";     // 用户的Access Key ID
        String secretAccessKy = "92f26f37a00b4e93aed8d381024088ec";  // 用户的Secret Access Key
        BceClientConfiguration config = new BceClientConfiguration()
                .withCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy))
                .withEndpoint(endPoint);
        CdnClient cdnClient = new CdnClient(config);
        purgeUrl(cdnClient);
    }

    public static void prefetchUrl(CdnClient client) {
        String domain = "https://www.1blv.cn/";

        // 用法2
        PrefetchResponse prefetchResponse = client.prefetch(domain);
        System.out.println(prefetchResponse);
    }

    public static void purgeUrl(CdnClient client) {
        String domain = "https://www.1blv.cn/";
        PurgeRequest request = new PurgeRequest()
                .addTask(new PurgeTask().withDirectory( domain));
        PurgeResponse purgeResponse = client.purge(request);
        System.out.println(purgeResponse);
        // 根据任务id查询刷新结果
        GetPurgeStatusResponse purgeStatusResponse = client.getPurgeStatus(
                new GetPurgeStatusRequest().withId(purgeResponse.getId()));
        System.out.println(purgeStatusResponse);
    }
}
